package com.jmp.security.grant.wx;

import com.jmp.security.base.ClientDetail;
import com.jmp.security.base.UserDetail;
import com.jmp.security.base.UserDetailService;
import one.stand.dto.WechatLoginDto;
import one.stand.model.User;
import one.stand.service.UserService;
import one.stand.service.user.UserAdminService;

public class WxImgUserDetailServiceImpl<T extends WxImgRequestDetail> implements UserDetailService<T> {
    private WxLoginService wxLoginService;
    private UserAdminService userAdminService;
    private UserService userService;

    public WxImgUserDetailServiceImpl(WxLoginService wxLoginService, UserAdminService userAdminService, UserService userService) {
        this.wxLoginService = wxLoginService;
        this.userAdminService = userAdminService;
        this.userService = userService;
    }

    @Override
    public UserDetail get(T requestDetail, ClientDetail clientDetail) {
        WechatLoginDto wechatLoginDto = wxLoginService.loginBack(requestDetail.getCode(),
                clientDetail.getCompanyAccountModel().getAppId(),
                clientDetail.getCompanyAccountModel().getAppSecret());
        String unionid = wechatLoginDto.getUnionId();
        User userModel = userService.getByUnionid(unionid);

        UserDetail userDetail = new UserDetail();
        userDetail.setUserModel(userModel);
        return userDetail;
    }
}
